//
//  LoginSetPasswordView.m
//  ZITHER
//
//  Created by weixin on 2019/1/17.
//  Copyright © 2019 weixn. All rights reserved.
//

#import "LoginSetPasswordView.h"
#import "LoginSetPasswordV.h"
#import "MeRegisterRegisterVC.h"

@interface LoginSetPasswordView()
@property (nonatomic, strong) LoginSetPasswordV *LoginSetPasswordV;
@property (nonatomic, strong) UIView *dimBackgroundView;/**< 半透明黑色背景 */
@property (nonatomic, strong) UIWindow *window;
@end


@implementation LoginSetPasswordView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
      
    }
    return self;
}
#pragma mark - getter
-(LoginSetPasswordV *)LoginSetPasswordV{
    if (!_LoginSetPasswordV) {
        _LoginSetPasswordV = [[LoginSetPasswordV alloc]init];
        __weak __typeof(self)weakSelf = self;
        [_LoginSetPasswordV setBlockHide:^(NSInteger index) {
            [weakSelf hide];

            if (index==1) {
    
                MeRegisterRegisterVC * vc = [[MeRegisterRegisterVC alloc]init];
                vc.state = MeRegisterRegisterStateSetPassword;
                UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
                
                UIViewController *currentVC = [Tools getCurrentVCFrom:rootViewController];
                [currentVC.navigationController  pushViewController:vc animated:YES];
//                if (weakSelf.nav == nil){
//                    [weakSelf.getNav pushViewController:vc animated:YES];
//                }else{
//                    [weakSelf.nav pushViewController:vc animated:YES];
//                }
                
            }
            
        }];
        
        
    }
    return _LoginSetPasswordV;
}

- (UIView *)dimBackgroundView
{
    if (!_dimBackgroundView) {
        _dimBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0 )];
        _dimBackgroundView.frame = CGRectMake(0, 0, kScreenW, kScreenH);
        _dimBackgroundView.backgroundColor = [UIColor blackColor];
        _dimBackgroundView.alpha = 0;
        
        // 添加手势监听
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)];
        [_dimBackgroundView addGestureRecognizer:tap];
    }
    return _dimBackgroundView;
}



- (void)show
{
    [self addToKeyWindow];
    [self showAnimationWithCompletion:nil];
}

- (void)hide
{
    [self hideAnimationWithCompletion:^(BOOL finished) {
        [self removeFromKeyWindow];
    }];
}


#pragma mark - private method

- (void)addToKeyWindow
{
    if (!self.superview) {
        UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
        [keyWindow addSubview:self];
        
        [self addSubview:self.dimBackgroundView];
        [self addSubview:self.LoginSetPasswordV];
        [self.LoginSetPasswordV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(30);
            make.right.equalTo(self).offset(-30);
            make.centerY.equalTo(self.mas_centerY).offset(0);
            make.height.mas_equalTo((kScreenW-60)/319*351);
        }];
    }
}

- (void)removeFromKeyWindow
{
    if (self.superview) {
        [self removeFromSuperview];
    }
}

- (void)showAnimationWithCompletion:(void (^)(BOOL finished))completion
{
    [UIView animateWithDuration:0.3 animations:^{
        self.dimBackgroundView.alpha = 0.3;
    } completion:completion];
}

- (void)hideAnimationWithCompletion:(void (^)(BOOL finished))completion
{
    [UIView animateWithDuration:0.3 animations:^{
        self.dimBackgroundView.alpha = 0;
    } completion:completion];
}


@end
